﻿/*!
 * Copyright 2008-2011 the original author or authors.
 * Licensed under the Apache License, Version 2.0
 */
  
/**
 * 项目监控页面：
 * 左边显示项目列表树；
 * 右上显示项目进度信息、右中显示业务功能按钮、右下显示业务记录；
 * 右边第二个标签页显示项目基本信息；右边第三个标签页显示投资计划信息；
 * 
 * @author TonyTan
 * @version 1.0, 2011-11-18
 */

Jxstar.currentPage = function(define, myProjectId) {
	//通过我关注的项目打开此功能时，才会有项目ID
	var pageParam = null;
	if (myProjectId != null) {
		pageParam = {};
		pageParam.pageType = 'editgrid';
		pageParam.whereSql = 'project_id = ?';
		pageParam.whereValue = myProjectId;
		pageParam.whereType = 'string';
		pageParam.isQuery = '1';//设置它，表示打开监控界面后还可以继续查询
	}
	var curProjectId = '';
	
	//显示指定功能的表格
	var showData = function(funId) {
		if (curProjectId.length == 0) return;
		var panel = Ext.getCmp('PanelMonitorGrid');
		panel.removeAll(true);
		//由于IE中显示隐藏Panel中的数据会报错，所以分开处理
		var showGrid = function(funId, dataId) {
			var define = Jxstar.findNode(funId);
			panel.setTitle(define.nodetitle);
			var params = {};
			params.pageType = 'query';
			params.whereSql = 'project_id = ?';
			params.whereValue = dataId;
			params.whereType = 'string';
			Jxstar.createPage(funId, 'gridpage', panel, params);
		};
		//如果隐藏了，则自动展开
		if (panel.collapsed) {
			panel.expand(true);
			JxUtil.delay(500, function(){showGrid(funId, curProjectId);});
		} else {
			showGrid(funId, curProjectId);
		}
	};
	
	var btndef = {disabled:true, iconCls:'eb_monno', xtype:'button', margins:'5', width:150};
	//添加项目过程相关按钮
	var btnPanel = new Ext.Panel({
		layout: 'anchor',
		frame:true,
		items:[{
			anchor: '100%',
			layout:'hbox',
			defaults:btndef,
			items: [{
				id: 'jkbtn_project_create',
				text: '项目立项审批'
			},{
				id: 'jkbtn_project_process',
				text: '项目进度计划',
				handler: function(){showData('project_process');}
			},{
				id: 'jkbtn_project_designchg',
				text: '工程量变更',
				handler: function(){showData('project_designchg');}
			},{
				id: 'jkbtn_project_mplan',
				text: '进度甘特图'
			}]
		},{
			anchor: '100%',
			baseCls:'x-plain',
			layout:'hbox',
			defaults:btndef,
			items: [{
				id: 'jkbtn_sj_contract_app',
				text: '合同审签申请',
				handler: function(){showData('sj_contract_app');}
			},{
				id: 'jkbtn_sj_contract_get',
				text: '合同审计意见',
				handler: function(){showData('sj_contract_get');}
			},{
				id: 'jkbtn_sj_balance',
				text: '结算审计申请',
				handler: function(){showData('sj_balance');}
			},{
				id: 'jkbtn_sj_balance_get',
				text: '结算审计定案',
				handler: function(){showData('sj_balance_get');}
			}]
		},{
			anchor: '100%',
			baseCls:'x-plain',
			layout:'hbox',
			defaults:btndef,
			items: [{
				id: 'jkbtn_pur_apply',
				text: '招投标采购单',
				handler: function(){showData('pur_apply');}
			},{
				id: 'jkbtn_contract_sign',
				text: '合同签订记录',
				handler: function(){showData('contract_sign');}
			},{
				id: 'jkbtn_pay_plan',
				text: '合同付款记录',
				handler: function(){showData('pay_plan');}
			},{
				id: 'jkbtn_contract_bill',
				text: '发票记录',
				handler: function(){showData('contract_bill');}
			},{
				id: 'jkbtn_project_check',
				text: '项目竣工验收',
				handler: function(){showData('project_check');}
			}]
		}]
	});
	
	//显示按钮的可操作性，体现项目进度；
	//在project_done功能的表格点击事件中触发执行；
	var showButtonData = function(projectId) {
		//给全局变量赋值
		curProjectId = projectId;
		
		var params = 'funid=project_done';
		params += '&pagetype=editgrid&eventcode=querydata&projectid=' + projectId;

		//生成文件后自动加载该文件
		var hdcall = function(data) {
			for(key in data){
				var jkbtn = Ext.getCmp('jkbtn_' + key);
				if (jkbtn) {
					var num = data[key];
					if (num.length == 0 || num == 0) {
						jkbtn.setIconClass('eb_monno');
						jkbtn.setDisabled(true);
					} else {
						jkbtn.setIconClass('eb_mon');
						jkbtn.setDisabled(false);
					}
					
					//处理立项申请的按钮
					if (key == 'project_create') {
						var createId = data.createFunId;
						jkbtn.handler = function(){showData(createId);}
					}
					
					//处理进度甘特图
					if (key == 'project_mplan') {
						var mplanid = data['project_mplan'];
						jkbtn.handler = function(){Jxstar.createNode('project_gantt', mplanid);}
					}
				}
			}
		};
		Request.postRequest(params, hdcall);
	};
	
	//1、创建右边第一个项目监控面板
	var panelMonitor = new Ext.Panel({
		border:false,
		layout:'border',
		items:[{
			region:'center',
			layout:'border',
			border:false,
			items:[{
				region:'center',
				layout:'fit',
				border:false
			},{
				region:'south',
				layout:'fit',
				height:150,
				border:false,
				items:[btnPanel]
			}]
		},{
			id:'PanelMonitorGrid',
			title:'请选择项目相关业务',
			animCollapse:true,
			collapsed:true,
			collapsible:true,
			region:'south',
			height:250,
			layout:'fit'
		}]
	});
	//取项目监控表格的面板
	var parentGrid = panelMonitor.getComponent(0).getComponent(0);
	//处理项目进度按钮的方法作为面板的成员方法，方便在项目进度数据加载时执行
	parentGrid.showButtonData = showButtonData;
	
	//添加项目监控进度表格
	Jxstar.createPage('project_done', 'gridpage', parentGrid, pageParam);

	//2、创建右边多标签页Tab控件
	var tabGridForm = new Ext.TabPanel({
		border:false,
		closeAction:'close',
		activeTab:0,
		items:[{
			pagetype:'grid',
			title: '项目监控',		//项目相关业务记录
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_grid',
			items:[panelMonitor]
		},{
			pagetype:'pro_form',
			title: '项目信息表单',	//项目信息表单
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_form'
		},{
			pagetype:'plan_form',
			title: '投资计划表单',	//投资计划表单
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_form'
		}]
	});
	//添加项目信息表单
	Jxstar.createPage('project_done', 'formpage', tabGridForm.getComponent(1));
	//添加投资计划表单
	Jxstar.createPage('money_query', 'formpage', tabGridForm.getComponent(2));
	//处理项目信息表单数据
	tabGridForm.on('beforetabchange', function(tabPanel, newTab, currentTab){
		var pagetype = newTab.pagetype;
		if (pagetype != 'grid' && curProjectId.length == 0) {
			JxHint.alert('请选择一条项目记录！');
			return false;
		}
	});
	tabGridForm.on('tabchange', function(tabPanel, activeTab){
		var pagetype = activeTab.pagetype;
		if (pagetype == 'grid') return;
		var grid = parentGrid.getComponent(0);
		var records = grid.getSelectionModel().getSelections();
		var curPage = activeTab.getComponent(0);
		var form = curPage.getForm();
		//显示项目信息
		if (pagetype == 'pro_form') {
			var record = records[0];
			form.myGrid = grid;
			form.myStore = grid.getStore();
			form.myRecord = record;
			form.loadRecord(record);
		} else {
		//加载投资计划数据
			var options = {
				where_sql: 'plan_status < ? and project_id = ?',
				where_type: 'string;string',
				where_value: '71;' + curProjectId,
				callback: function(data) {
					if (data.length > 0) {
						var r = curPage.formNode.event.newRecord(data[0]);
						
						curPage.getForm().myRecord = r;
						curPage.getForm().loadRecord(r);
					}
				}
			};
			Jxstar.queryData('money_query', options);
		}
	});

	return tabGridForm;
};